package com.bjfu.intelligentrecognizeserver.service.impl;

import com.bjfu.intelligentrecognizeserver.common.constant.MessageConstant;
import com.bjfu.intelligentrecognizeserver.common.exception.AccountNotFoundException;
import com.bjfu.intelligentrecognizeserver.common.exception.PasswordErrorException;
import com.bjfu.intelligentrecognizeserver.context.BaseContext;
import com.bjfu.intelligentrecognizeserver.mapper.UserMapper;
import com.bjfu.intelligentrecognizeserver.pojo.dto.UserLoginDTO;
import com.bjfu.intelligentrecognizeserver.pojo.dto.UserRegisterDTO;
import com.bjfu.intelligentrecognizeserver.pojo.entity.User;
import com.bjfu.intelligentrecognizeserver.pojo.vo.UserInfoVo;
import com.bjfu.intelligentrecognizeserver.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
@Slf4j
public class UserServiceImpl implements UserService {
    private final UserMapper userMapper;

    @Override
    public User login(UserLoginDTO userLoginDTO) {
        Integer type = userLoginDTO.getType();
        String usernameOrPhone = userLoginDTO.getUsernameOrPhone();
        String password = userLoginDTO.getPassword();
        User user;//密码错误
        //账号不存在
        if (type == 1) {
            //手机号登录
            //查询当前手机号是否存在
            user = userMapper.selectUserByPhone(usernameOrPhone);
        } else {
            //用户名登录
            user = userMapper.selectUserByUsername(usernameOrPhone);
        }
        if (user == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        if (!user.getPassword().equals(password)) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        return user;
    }

    @Override
    public int register(UserRegisterDTO userRegisterDTO) {
        //判断当前用户是否已经注册
        String username = userRegisterDTO.getUsername();
        String phone = userRegisterDTO.getPhone();
        //已经注册
        if (userMapper.selectUserByPhone(phone) != null || userMapper.selectUserByUsername(username) != null) {
            return 0; //当前用户已经存在，注册失败
        }
        userMapper.register(new User(
                null,
                username,
                phone,
                userRegisterDTO.getPassword(),
                null,
                LocalDateTime.now(),
                LocalDateTime.now()
        ));
        return 1;
    }

    @Override
    public UserInfoVo getUserInfo() {
        //获取当前用的的id
        Integer userId = BaseContext.getCurrentId();
        log.info("user id is {}", userId);
        User user = userMapper.selectUserById(userId);
        return UserInfoVo.builder()
                .image(user.getImage())
                .username(user.getUsername())
                .phone(user.getPhone())
                .password(user.getPassword()).build();
    }
}
